// ------------------------------------------------------------------------------------------
// Licensed by Interprise Solutions.
// http://www.InterpriseSolutions.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using InterpriseSuiteEcommerceCommon.DataAccess;

namespace InterpriseSuiteEcommerceCommon.DTO
{
    public class PaymentTermDTO
    {
        private string _paymentTermCode;
        private string _paymentMethod;
        private string _description;
        private bool _isActive;
        private bool _isSelected;

        public EventHandler SelectedChanged;

        public PaymentTermDTO(string code, string method, string description, bool active)
        {
            _paymentTermCode = code;
            _paymentMethod = method;
            _description = description;
            _isActive = active;
        }

        public string PaymentTermCode
        {
            get { return _paymentTermCode; }
        }

        public string PaymentMethod
        {
            get { return _paymentMethod; }
        }

        public string Description
        {
            get { return _description; }
        }

        public bool IsActive
        {
            get { return _isActive; }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                OnSelectedChanged();
            }
        }

        private void OnSelectedChanged()
        {
            if (null != this.SelectedChanged)
            {
                this.SelectedChanged(this, EventArgs.Empty);
            }
        }

        public static PaymentTermDTO Find(string withCode)
        {
            PaymentTermDTO term = null;

            using (SqlConnection con = DB.NewSqlConnection())
            {
                con.Open();
                using (IDataReader reader = DB.GetRSFormat(con, "exec eCommerceGetPaymentTerm @PaymentTermCode = {0}", DB.SQuote(withCode)))
                {
                    if (reader.Read())
                    {
                        term = new PaymentTermDTO(DB.RSField(reader, "PaymentTermCode"),
                                DB.RSField(reader, "PaymentMethodCode"),
                                DB.RSField(reader, "PaymentTermDescription"),
                                true);

                        term.IsSelected = true; //not needed here anyway...
                    }
                }
            }

            return term;
        }

        public static IEnumerable<PaymentTermDTO> GetAllForGroup(string ofContact, Address preferredShippingAddress)
        {
            return CustomerDA.GetAllForGroup(ofContact, preferredShippingAddress);
        }

        public class PaymentTermGroup : List<PaymentTermDTO>
        {
            public void AddTerm(PaymentTermDTO term)
            {
                term.SelectedChanged += PaymentTerm_SelectedChanged;
                if (term.PaymentMethod == InterpriseHelper.PAYMENT_METHOD_CREDITCARD &&
                    !term.PaymentTermCode.Equals("PURCHASE ORDER", StringComparison.InvariantCultureIgnoreCase) && 
                    !term.PaymentTermCode.Equals("REQUEST QUOTE", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.Insert(0, term);
                    term.IsSelected = true;
                }
                else
                {
                    this.Add(term);
                }
            }

            private void PaymentTerm_SelectedChanged(object sender, EventArgs e)
            {
                PaymentTermDTO term = sender as PaymentTermDTO;
                if (term.IsSelected)
                {
                    foreach (PaymentTermDTO otherTerm in this)
                    {
                        if (otherTerm != term)
                        {
                            otherTerm.IsSelected = false;
                        }
                    }
                }
            }

            public void EnsureHasSelected()
            { 
            }
            
        }
    }
}






